//package com.jic.market.filter;
//import com.alibaba.fastjson.JSON;
//import com.alibaba.fastjson.JSONObject;
//import com.google.common.base.Splitter;
//import com.jic.manager.api.rest.SysUserApi;
//import com.jic.market.holder.UserThreadLocalHolder;
//import com.jic.market.model.LoginUser;
//import org.apache.commons.lang.StringUtils;
//import org.springframework.stereotype.Component;
//
//import javax.annotation.Resource;
//import javax.servlet.*;
//import javax.servlet.annotation.WebFilter;
//import javax.servlet.http.HttpServletRequest;
//import java.io.IOException;
//
//
//@Component
//@WebFilter(urlPatterns = "/*",filterName = "channelFilter")
//public class UserTokenFilter implements Filter {
//    @Resource
//    private SysUserApi sysUserApi;
//
//    @Override
//    public void init(FilterConfig filterConfig) throws ServletException {
//    }
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        ServletRequest requestWrapper = null;
//        if(servletRequest instanceof HttpServletRequest) {
//            requestWrapper = new RequestWrapper((HttpServletRequest) servletRequest);
//        }
//        if(requestWrapper == null) {
//            filterChain.doFilter(servletRequest, servletResponse);
//        } else {
//            try {
//                String token = null;
//                LoginUser user = new LoginUser();
//                String body = ((RequestWrapper) requestWrapper).getBody();
//                if(StringUtils.isNotBlank(body)){
//                    try {
//                        JSONObject jsonObject = JSON.parseObject(body);
//                        token = jsonObject.getString("token");
//                    } catch (Exception e) {
//                        token = Splitter.on("&").withKeyValueSeparator("=").split(body).get("token");
//                    }
//                }
//                if(token == null){
//                    token = requestWrapper.getParameter("token");
//                }
//                if(token != null){
//                    Object data = sysUserApi.getSysUserByToken(token).getData();
//                    if(data != null){
//                        user = JSON.parseObject(JSON.toJSONString(data),LoginUser.class);
//                    }
//                }
//                UserThreadLocalHolder.USER_TOKEN.set(token);
//                UserThreadLocalHolder.CURRENT_USER.set(user);
//                filterChain.doFilter(requestWrapper, servletResponse);
//            } catch (Exception e) {
//                throw e;
//            } finally {
//                UserThreadLocalHolder.USER_TOKEN.remove();
//                UserThreadLocalHolder.CURRENT_USER.remove();
//            }
//        }
//    }
//    @Override
//    public void destroy() {
//
//    }
//}
